package com.organization

import grails.test.*

class EmployeeServiceTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testServiceMethods() {
        //create testing service
        def testService = new EmployeeService()

        //mock commonService methods
        def mockControl = mockFor(CommonService,true)
        mockControl.demand.nodeIdToDbId(4..4) {nodeId->
            nodeId.replaceAll("[a-zA-Z/_]","")
        }
        mockControl.demand.convertDateFromJson(6..6) {date->
            new Date()
        }
        testService.commonService = mockControl.createMock()

        //mock domain for tests
        mockDomain(Department)
        def testDep = new Department(id: 1, name:"Dep1")
        assertNotNull "can't save department",testDep.save()
        def proj1 = new Project(id: 1, department: testDep, name: "Proj1", startDate: new Date())
        mockDomain(Project,[proj1])
        def empl1 = new Employee(id: 1, project: proj1, firstName: "Ivan", lastName: "Ivanov", hireDate: null, birthDate: null)
        mockDomain(Employee,[empl1])

        //test getEmployeeInfo(String emplId)
        def result = testService.getEmployeeInfo("Employee_1")
        assertNotNull "null result in getDepartmentInfo",result
        assertEquals "true",result.success
        assertEquals "Ivanov",result.lastName

        //test updateEmployeeInfo(params)
        result = testService.updateEmployeeInfo([nodeId: "Employee_1", lastName: "Petrov", firstName: "Petr", hireDate: null,birthDate: null])
        assertNotNull "null result in updateEmployeeInfo",result
        assertNotNull "newName field is null",result.newName
        assertNotNull "nodeId field is null",result.nodeId
        assertEquals "true",result.success
        def foundEmpl = Employee.findById(1)
        assertNotNull "found employee is null",foundEmpl
        assertEquals "Petrov",foundEmpl.lastName

        //test addEmployee(params)
        result = testService.addEmployee([parentNodeId: "Project_1", lastName: "Nesterov", firstName: "Igor", hireDate: null, birthDate: null])
        assertNotNull "null result in addEmployee",result
        assertEquals "Project_1",result.parentNodeId
        assertNotNull "nodeId field is null",result.nodeId
        assertEquals "true",result.success

        foundEmpl = Employee.findById(1)
        assertNotNull "found employee is null",foundEmpl
        assertEquals "Petrov",foundEmpl.lastName
        assertEquals 2,Employee.findAll().size()

        //test deleteEmployee(params)
        result = testService.deleteEmployee([nodeId: "Employee_1",])
        assertNotNull "null result in deleteEmployee",result
        assertEquals "true",result.success

        foundEmpl = Employee.findById(1)
        assertNull "employee was not deleted",foundEmpl
        assertEquals 1,Employee.findAll().size()

        //verify count of mocked methods calls
        mockControl.verify()
    }
}
